
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GeoBox.h"

#include <math.h>
#include "OGLVisu.h"
#include <QGLWidget>

#include "CMatrix.h"

//constructor
GeoBox::GeoBox() : Geometry(GT_BOX), _width(1), _height(1), _depth(1)
{
	_rotationMarkRadius = 1.42;
	_translateMarkOffset = 0.5f;
	_translateMark = CVector3f(0, 0, 0);
	_translateMark.y = 0.5f + _translateMarkOffset;
}

//destructor
GeoBox::~GeoBox(){}

//determine if the point v is a point of the geometry object
bool GeoBox::isGeometry(const CVector3f& v)const
{
	//calculate the world matrix of the primitive
	CMatrix t;
	t.createTranslation(_translate);

	CMatrix rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	CMatrix rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	CMatrix rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	CMatrix s;
	s.createScale(_scale);

	CMatrix transform = s * rZ * rY * rX * t;

	//get V in local coordinate of the sphere
	CVector3f localV = v * transform.inverse();

	//test if inside
	float error = 0.1f;
	float halfWidth = _width/2 + error;
	float halfHeight = _height/2 + error;
	float halfDepth = _depth/2 + error;
	if (localV.x < -halfWidth || localV.x > halfWidth ||
		localV.y < -halfHeight || localV.y > halfHeight ||
		localV.z < -halfDepth || localV.z > halfDepth)  //too far outside
		return false;

	halfWidth = _width/2 - error;
	halfHeight = _height/2 - error;
	halfDepth = _depth/2 - error;
	if (localV.x < halfWidth && localV.x > -halfWidth &&
		localV.y < halfHeight && localV.y > -halfHeight &&
		localV.z < halfDepth && localV.z > -halfDepth) //too much inside
		return false;

	return true;
}

//set the sphere radius
void GeoBox::setDimensions(float w, float h, float d)
{
	_width = w;
	_height = h;
	_depth = d;

	_rotationMarkRadius = _width > _height ? _width : _height;
	_rotationMarkRadius = _rotationMarkRadius > _depth ? _rotationMarkRadius : _depth;

	_rotationMarkRadius = _rotationMarkRadius/2.f * sqrt(2.f);
	_translateMark.y = _height / 2.f + _translateMarkOffset;

	
}

//render the object
void GeoBox::Render()
{
	VISU.drawBox(_width, _height, _depth);
}

//post render function
void GeoBox::postRender()
{
	//if the sphere is selected, draw the surrounding box and the translation mark
	if(_selected)
	{
		VISU.drawSelectionBox(_width+1, _height+1, _depth+1);
	}

	glPopMatrix();
}

//width getter
float GeoBox::getWidth()const
{
	return _width;
}

//height getter
float GeoBox::getHeight()const
{
	return _height;
}

//depth getter
float GeoBox::getDepth()const
{
	return _depth;
}